package com.esun.cheetah.biz.personal.attention;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.Handler;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.model.personal.attention.AttentionGameInfo;
import com.esun.cheetah.network.AbsInferiorityCommunicater;

/**
 * @title AbsAttentionMatchListCommunicater
 * @description 关注比赛列表的抽象通信类
 * @author liugan
 */
public abstract class AbsAttentionMatchListCommunicater extends
		AbsInferiorityCommunicater {

	/** 关注比赛信息List */
	protected ArrayList<AttentionGameInfo> attentionMatchList;

	public AbsAttentionMatchListCommunicater(Handler handler) {
		super(handler);
	}

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> gainUserAttentionMap = new HashMap<String, String>();
		gainUserAttentionMap.put(Key.KEY_ID, BizCode.BIZCODE_GAIN_MATCH);
		gainUserAttentionMap.put(Key.KEY_USERNAME, UserInfo.getUserInfo()
				.getUserName());
		
		gainUserAttentionMap.put(Key.KEY_LOTID, Key.ZQ);

		return gainUserAttentionMap;
	}

	protected List<AttentionGameInfo> getListAttentionGame() {
		return attentionMatchList;
	}

	@Override
	public void getJsonContent(String serverRes) throws JSONException {
		JSONObject object = (JSONObject) new JSONTokener(serverRes).nextValue();
		JSONArray json = object.getJSONArray("data");
		// [{"id":"28","away":"加拉茨钢铁","home":"阿斯特拉","matchid":"346827","lotid":"zq","username":"elot","begintime":"0000-00-00 00:00:00"},{"id":"27","away":"特纳瓦斯巴达","home":"斯利纳","matchid":"326228","lotid":"zq","username":"elot","begintime":"0000-00-00 00:00:00"},{"id":"26","away":"阿尔费萨里","home":"巴卡","matchid":"347590","lotid":"zq","username":"elot","begintime":"0000-00-00 00:00:00"},{"id":"25","away":"雅莫科","home":"加里里","matchid":"347591","lotid":"zq","username":"elot","begintime":"0000-00-00 00:00:00"},{"id":"24","away":"弗拉基米尔鱼雷","home":"卡希梅基","matchid":"354572","lotid":"zq","username":"elot","begintime":"0000-00-00 00:00:00"}]
		attentionMatchList = new ArrayList<AttentionGameInfo>();
		for (int i = 0; i < json.length(); i++) {
			JSONObject match = json.getJSONObject(i);
			AttentionGameInfo attenGame = new AttentionGameInfo();
			attenGame.setId(match.getString(Key.KEY_ID));
			attenGame.setMatchid(match.getString(Key.KEY_MATC_ID));
			attenGame.setLotid(match.getString(Key.KEY_LOTID));
			attenGame.setLeagueName(match.getString(Key.KEY_LEAGUENAME));
			attenGame.setHome(match.getString(Key.KEY_HOME));
			attenGame.setHomeNumber(match.getString(Key.KEY_HOME_STANDING));
			attenGame.setHomeScore(match.getString(Key.KEY_HOME_SCORE));
			attenGame.setAway(match.getString(Key.KEY_AWAY));
			attenGame.setAwayNumber(match.getString(Key.KEY_AWAY_STANDING));
			attenGame.setAwayScore(match.getString(Key.KEY_AWAY_SCORE));
			attenGame.setRound(match.getString(Key.KEY_MATCH_ROUND));
			attenGame.setStage(match.getString(Key.KEY_STAGE_NAME));
			attenGame.setBegintime(match.getString(Key.KEY_BEGINTIME));
			attenGame.setShowState(getShowState());
			final int status = match.getInt(Key.KEY_STATUS);
			final String matchTime = match.getString(Key.KEY_TIMEING);
			setMatchState(status, matchTime, attenGame);

			attentionMatchList.add(attenGame);
		}
	}

	/**
	 * @param matchTime
	 * @title setMatchState
	 * @description 设置比赛的状态描叙
	 * @param int1
	 * @param attenGame
	 */
	private void setMatchState(int status, String matchTime,
			AttentionGameInfo attenGame) {
		switch (status) {
		case 0:
			attenGame.setStateDescription(attenGame.getBegintime() + Language.CHANGE_LINE+Language.START_MATCH);
			break;
		case 1:
			attenGame.setStateDescription(Language.FIRST_HALF+ Language.CHANGE_LINE + matchTime + Language.MINUTE);
			break;
		case 2:
			attenGame.setStateDescription(Language.HALF_TIME);
			break;
		case 3:
			attenGame.setStateDescription(Language.SECOND_HALF+ Language.CHANGE_LINE + matchTime + Language.MINUTE);
			break;
		case 4:
			attenGame.setStateDescription(Language.GAME_OVER);
			break;
		case 5:
			attenGame.setStateDescription(Language.MATCH_CANCEL);
			break;
		case 6:
			attenGame.setStateDescription(Language.MATCH_CHANGE_TIME);
			break;
		case 7:
			attenGame.setStateDescription(Language.MATCH_ABORT);
			break;
		case 8:
			attenGame.setStateDescription(Language.MATCH_INTERRUPT);
			break;
		case 9:
			attenGame.setStateDescription(Language.MATCH_UP_IN_THE_AIR);
		}

	}

	/**
	 * @title getShowState
	 * @description 获取用于显示的比赛状态状态
	 * @return
	 */
	protected abstract int getShowState();

	public ArrayList<AttentionGameInfo> getMatchList() {
		return attentionMatchList;
	}

}
